/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

/**
 * SmsLog.java
 * This class holds connections, if connection is broken then it recreates it
 */

import java.sql.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import blueprint4j.utils.*;

public class SmsLog extends Entity {

	//static String VERSION = "$Id: SmsLog.java,v 1.4 2004/08/05 11:38:08 jaspervdb Exp $";

	public static final String TABLE_NAME = "d_sms_log";
	public TableName table_name = new TableName(TABLE_NAME, this);
	
	private static SMSCost sms_cost = null;
	
	public FieldUnique id = new FieldUnique("id", 0, "The Record Identifier", this);
	
	public FieldGroup group_sms = new FieldGroup("SMS",this);
	
	public FieldString sms = new FieldString("sms",Field.FIELD_NAME, "SMS",160, this);
	public FieldString msisdn = new FieldString("msisdn",Field.FIELD_NAME, "msisdn",20, this);
	public FieldCharacter flag = new FieldCharacter("flag",0, "Out or In", this);
	public FieldDateTime datetime = new FieldDateTime("datetime",0,"",this);
	public FieldCurrency cost = new FieldCurrency("cost",0,"The Cost of the sms",this);
	public FieldBoolean processed = new FieldBoolean("processed",0,"Processed",this);

	public static final String DBVERSIONPREFIX = "db_tools.dataversion";
	public static List<String> upgrades = new ArrayList<String>();

	//private Maintenance maintenance = new Maintenance(this,new Maintenance.Action[]{new Maintenance.ActionTrim(500000)},60*1000*60*5);
		  

	public SmsLog() {super();}

	public SmsLog (DBConnection connection) throws DataException, SQLException {
		super(connection);
	}

	public SmsLog (DBConnection connection, ResultSet result_set)
	throws DataException, SQLException {
		super(connection);
		load(result_set);
	}

	public static void setSMSCost(SMSCost p_sms_cost) {
		sms_cost = p_sms_cost;
	}
	
	public Entity getNewInstance() {
		return new SmsLog();
	}

	private static void newSms(String msisdn,String msg,Character direction)
	throws SQLException,BindException {
		SmsLog sms = new SmsLog(DBTools.getDLC());
		sms.msisdn.set(msisdn);
		sms.sms.set(msg);
		sms.flag.set(direction);
		sms.datetime.set(new java.util.Date());
		if (direction.charValue() == 'O') {
			sms.cost.set(new Currency(""+(Settings.getInt("sms.cost",100)/100.0)));
		} else {
			if (sms_cost != null) {
				sms.cost.set(sms_cost.getCost(msisdn));
			}
		}
		sms.save();
	}
	
	public static void newSmsDelivered(String msisdn,String msg)
	throws SQLException,BindException {
		newSms(msisdn,msg,new Character('O'));
	}
	
	public static void newSmsArrived(String msisdn,String msg)
	throws SQLException,BindException {
		newSms(msisdn,msg,new Character('I'));
	}
	
	public static interface SMSCost {
		public Currency getCost(String msisdn);
	}
	
}
